/**
 *	插件:短信使用
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */


package com.framework.Chajian;


import com.framerock.db.DBConn;
import com.framerock.server.Response;
import com.framerock.baselib.CLog;
import com.framerock.baselib.CallBack;
import com.framerock.baselib.Jiami;
import com.framerock.baselib.Rock;
import com.framerock.baselib.RockHttp;
import com.framerock.baselib.RockMsg;
import com.framerock.baselib.Xinhu;

import java.util.Map;

public class ChajianSmsXinhu extends ChajianSms {

	//接口地址不可修改
    private String apiurl = "aHR0cDovL2FwaS5yb2Nrb2EuY29tLw::";

    public ChajianSmsXinhu(DBConn dbs) {
        super(dbs);
        this.apiurl = Jiami.base64decode(this.apiurl);
    }

    /**
     * 获取验证码
     * */
    public String sendCode(String mobile, String key)
    {
        Map<String, String> params = Rock.getMap();
        RockMsg codeobj = getCode(mobile, key);
        if(codeobj.isError())return codeobj.back();
        String code = codeobj.getData();
        params.put("code", code);
        return send(mobile, "", "defyzm", params);
    }

    /**
     * 发送短信
     * */
    public String send(String mobile, String qianm, String tplnum, Map<String, String> params)
    {
        if(Rock.isEmpt(qianm))qianm = Xinhu.getVal(this.nDb, "smsqianm");
        if(Rock.isEmpt(qianm))qianm = "xinhuyun";

        String url = this.apiurl+"?m=sms&a=send";
        String xinhukey = Xinhu.getVal(this.nDb, "xinhukey");
        url += "&xinhukey="+ xinhukey+"";

        String canshu = "sys_tomobile="+mobile+"";
        canshu+="&sys_tplnum="+tplnum+"";
        canshu+="&sys_qiannum="+qianm+"";
        if(params!=null)for (Map.Entry<String, String> entry : params.entrySet()) {
            canshu+="&can_"+entry.getKey()+"="+entry.getValue()+"";
        }
        final String[] result = {""};
        RockHttp.sendurl(url,"POST",canshu, new CallBack(){
            @Override
            public void backhttp(int code, int arg1, String bstr) {
                result[0] = bstr;
            }
        });
        String cont = result[0];
        if(Rock.isEmpt(cont) || !cont.substring(0,1).equals("{")){
            cont = Response.returnError("apierror:"+cont+"");
        }else{
            if(Rock.contain(cont, "\"success\":true"))cont = Response.returnSuccess("ok");
        }
        return cont;
    }
}
